var app = new Vue({
      el: '#testAunt',
      data: {
        basicsInformationId: 0,
        score: 0,
        userScore:0,
        passLine: 80,
        papertype:'月嫂',
        level: '初级',
        controller: {
            isPhotoCon: false,
            alertText: false,
        },

        userInfo: {
            post:0,
            value:0,
            examinationItem: [
                {
                    status:0,
                    option:[]
                }
            ]
        },
        performance: {
            isPass: false,
            line1:'',
            line2:'',
            line3:'',
            line4:'',
            btn1:{
                isShow:true,
                text:'返回首页'
            },
            
        },
        navIndexAct:{
            ind:1,
            navTitle:[
                {title:'请选择您的工种与考核项目'},
                {title:'考试详择'},
                {title:'测评'},
                {title:'考核结果'}
               
            ]
        },
        questionList: [],
       
      },

      methods:{
        goHome:function() {
            window.location.href = 'index.html';
        },
        nextPage: function(){


            switch (this.navIndexAct.ind){
                    case 1:
                        $.ajax({
                            url: severUrl + 'getPaperById?id=' + app.userInfo.value,
                            type: 'GET',
                            dataType: 'JSON'
                        })
                        .done(function(data) {
                            console.log("success");
                            console.log(data);

                            app.questionList = data.data;
                            app.passLine = data.passline;
                            app.papertype = data.papertype;

                            if (data.level == 1) {
                                app.level = '初级';
                            }else if (data.level == 2) {
                                app.level = '中级';
                            }else if (data.level == 3) {
                                app.level = '高级';
                            }

                            app.navIndexAct.ind ++ ;

                        })

                      break;
                    case 2:
                        this.navIndexAct.ind ++ ;
                      break;
                    case 3:

                        // console.log(JSON.stringify(datainfo))
                        this.calculatePerformance();
                        this.calculateRate();

                        var datainfo = {
                                phone: loginObj.getLogoinInfo(),
                                questions: app.questionList,
                                // userScore: app.userScore,
                                paper_id: app.userInfo.value,
                                isThrough: app.performance.isPass ? 'Y' : "N",
                                score: app.score,

                            }

                            console.log(datainfo)
                            console.log(JSON.stringify(datainfo))

                        $.ajax({
                            url: severUrl+'addPaperUser',
                            type: 'POST',
                            dataType: 'JSON',
                            data: datainfo,
                        })
                        .done(function(data) {
                            console.log("success");
                            console.log(data);

                            if (data.success) {
                             app.navIndexAct.ind ++ ;

                            }
                        })
                        .fail(function(err) {
                            console.log("error");
                            console.log(err)
                        })
                        .always(function() {
                            console.log("complete");
                        });
                        
                                                
                                                

                      break;
                    
                }
        },
        calculatePerformance: function(){
            var list = this.questionList;
            for (var i = 0; i < list.length; i++) {
                if (typeof list[i].selected == 'object') {
                    if (list[i].selected.sort().toString() == list[i].correct.toString()) {
                        this.userScore++;

                    }
                }else{
                    if (list[i].selected == list[i].correct) {
                        this.userScore++;
                    }
                }   
                console.log(this.userScore);



            }


            this.score = 100/list.length * this.userScore;

        },


        AlertConFun: function(string) {
            this.controller.alertText = string;
            setTimeout(function(argument) {
                this.app._data.controller.alertText = false;
            },1500)

        },

        calculateRate: function(){
            if (this.score > this.passLine) {
                this.performance.isPass = true;
                this.performance.line1 = '恭喜你 !';
                this.performance.line2 = this.level+this.papertype+'考试获得'+this.score+'分';
                this.performance.line3 = '您已成为'+ this.level+this.papertype;

                this.performance.line4 = this.isNextGrade(this.level);

            }else{
               this.performance.isPass = false;
                this.performance.line1 = '很遗憾 !';
                this.performance.line2 = this.papertype+this.level+'考试获得'+this.score+'分';
                this.performance.line3 = '没有通过'+this.papertype+this.level+'考核';
                this.performance.line4 = '评级失败 ！';
 
            }

        },
        isNextGrade: function(string){

            if (string == '初级') {
                return '接下来进入中级考试';
            }else if (string == '中级') {
                return '接下来进入高级考试';
            }else if (string == '高级') {
                return '全部考核完成 ！';
            }
        }




      },
      watch: {
        'userInfo.post': function(val, oldVal){
            this.userInfo.value = this.userInfo.examinationItem[val].option[0].id;
        },

      },

      created:function () {
        var phone = loginObj.getLogoinInfo();
        

        $.ajax({
            url: severUrl + 'getPaperUser?phone=' + phone,
            type: 'GET',
            dataType: 'JSON'
        })
        .done(function(data) {
            console.log("success");
            console.log(data);

            app.userInfo.examinationItem = data;
            app.userInfo.value = data[app.userInfo.post].option[0].id;

        })


      }
})

